package logic

import (
	"context"
	"errors"
	"strings"
	"video-up-manager/models/mysql"

	"video-up-manager/videorpc/internal/svc"
	"video-up-manager/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentLogic {
	return &CommentLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommentLogic) Comment(in *videorpc.CommentRequest) (*videorpc.CommentResponse, error) {
	// todo: add your logic here and delete this line

	comment := mysql.Comment{
		VideoId:        in.VideoId,
		UserId:         in.UserId,
		CommentContent: in.CommentContent,
		IsAnonymous:    in.IsAnonymous,
		CommentTime:    in.CommentTime,
		CommentStatus:  in.CommentStatus,
		ReplyId:        in.ReplyId,
	}
	if in.CommentContent == "" {
		return nil, errors.New("请输入评论内容")
	}
	if strings.Contains(in.CommentContent, "垃圾") || strings.Contains(in.CommentContent, "骗人") {
		return nil, errors.New("含违规词汇，请修改")
	}
	if err := comment.CommentAdd(mysql.DB); err != nil {
		return nil, err
	}

	return &videorpc.CommentResponse{
		CommentId: int64(comment.ID),
	}, nil
}
